package com.kitty.game.feixingqi;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.base.service.BagService;
import com.kitty.game.config.OnlineMall;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.feixingqi.message.ReqQiling;
import com.kitty.game.feixingqi.message.ReqQiling2;
import com.kitty.game.fly.FlyTypeEnums;
import com.kitty.game.onlinemall.service.MallService;
import com.kitty.game.role.model.Role;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller
public class FeixingqiController {

    @Autowired
    EquipService equipService;
    /**
     * 启灵
     * @param session
     * @param reqQiling
     */
    @RequestMapping
    public void qiling(IoSession session, ReqQiling reqQiling) {

        Role role = SessionUtils.getRoleBySession(session);
        if (role == null){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("角色不存在"));
            return;
        }

        List<Short> posData = reqQiling.getData() ;
        for (Short pos : posData){
            RoleEquip roleEquip = equipService.getRoleEquipByPos(role, pos);
            if(roleEquip == null){
                MessagePusher.pushMessage(role,new RespNotifyMiscEx(roleEquip.getName()+"不存在"));
                return;
            }
            if (roleEquip.queryNum() < 1) {
                MessagePusher.pushMessage(role,new RespNotifyMiscEx(roleEquip.getName()+"数量不足"));
                return;
            }

            if(roleEquip.getName().contains("梦荷")|| roleEquip.getName().contains("御天梭")){

            }else {
                MessagePusher.pushMessage(role,new RespNotifyMiscEx(roleEquip.getName()+"材料异常，请联系管理员"));
                return;
            }

            equipService.delRoloEquip(roleEquip.getName(), 1, false, session);
        }


        OnlineMall onlineMall = DataCache.NAME_MALL.get(reqQiling.getItem_name());
        if (onlineMall == null) {
            onlineMall = new OnlineMall();
            onlineMall.setName(reqQiling.getItem_name());
            onlineMall.setItemtype((short) 34);
            onlineMall.setType(21);
        }

        short pos = SpringUtils.getBean(BagService.class).getPos(role, false);
        if (pos <= 0) {
            return;
        }

        SpringUtils.getBean(MallService.class).qiling(role,onlineMall,1, true, pos);


    }


    @RequestMapping
    public void qiling2(IoSession session, ReqQiling2 reqQiling) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("角色不存在"));
            return;
        }

        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, reqQiling.getMaterial_pos());
        if(roleEquip == null){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("御天梭不存在"));
            return;
        }
        if (roleEquip.queryNum() < 1) {
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("御天梭数量不足"));
            return;
        }

        if( roleEquip.getName().contains("御天梭")){

        }else {
            MessagePusher.pushMessage(role,new RespNotifyMiscEx(roleEquip.getName()+"材料异常，请联系管理员"));
            return;
        }

        equipService.delRoloEquip(roleEquip.getName(), 1, false, session);

        roleEquip = equipService.getRoleEquipByPos(role, reqQiling.getArtifact_pos());
        if(roleEquip == null){
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("启灵法宝不存在"));
            return;
        }
        if (roleEquip.queryNum() < 1) {
            MessagePusher.pushMessage(role,new RespNotifyMiscEx("启灵法宝数量不足"));
            return;
        }
        int type = FlyTypeEnums.getType(roleEquip.getName());
        if(type>=3){
        }else {
            MessagePusher.pushMessage(role,new RespNotifyMiscEx(roleEquip.getName()+"材料异常，请联系管理员"));
            return;
        }
        String hechengdaoju = roleEquip.getName() ;
        equipService.delRoloEquip(roleEquip.getName(), 1, false, session);



        OnlineMall onlineMall = DataCache.NAME_MALL.get(hechengdaoju);
        if (onlineMall == null) {
            onlineMall = new OnlineMall();
            onlineMall.setName(hechengdaoju);
            onlineMall.setItemtype((short) 34);
            onlineMall.setType(21);
        }

        short pos = SpringUtils.getBean(BagService.class).getPos(role, false);
        if (pos <= 0) {
            return;
        }

        SpringUtils.getBean(MallService.class).qiling(role,onlineMall,1, true, pos);


    }

}
